package com.ohos.carbon.slice.widgets;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;
import ohos.agp.components.element.ElementScatter;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;
public class ExpansionPanelSlice extends AbilitySlice {

    private Image back_button, image_expan;
    private Text text_title, text_content;
    private DependentLayout depen_btn;
    private StackLayout stackLayout;
    private boolean flag_layout = false;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_expansion_panel);
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0){
            if (MainAbilitySlice.all.containsKey("ExpansionPanel")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("ExpansionPanel")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("ExpansionPanel", integer);
                        pf.flushSync();
                    }
                }
            }else {
                pf.putInt("ExpansionPanel", 1);
                pf.flushSync();
            }
        }else {
            pf.putInt("ExpansionPanel", 1);
            pf.flushSync();
        }
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        depen_btn = (DependentLayout) findComponentById(ResourceTable.Id_depen_btn);
        stackLayout = (StackLayout) findComponentById(ResourceTable.Id_stack_layout);
        image_expan = (Image) findComponentById(ResourceTable.Id_image_expan);
        text_content = (Text) findComponentById(ResourceTable.Id_text_content);
        text_content.setText("Lorem ipsum dolor sit amet, consectetur" + "\n" +
                "adipiscing elit. Proin nibh augue, suscipit a," + "\n" + "scelerisque sed, lacinia in, mi. " +
                "Cras vel lorem." + "\n" + "Etiam pellentesque aliquet tellus. Phasellus" + "\n" + "pharetra nulla ac diam.");
        image_expan.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_arrow_down));
        text_title.setText("ExpansionPanel");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        depen_btn.setClickedListener(component -> {
            if (flag_layout) {
                AnimatorProperty animator = image_expan.createAnimatorProperty();
                animator.rotate(0).setDuration(200).start();
                stackLayout.setVisibility(Component.VISIBLE);
                flag_layout = false;
            } else {
                AnimatorProperty animator = image_expan.createAnimatorProperty();
                animator.rotate(180).setDuration(200).start();
                stackLayout.setVisibility(Component.HIDE);
                flag_layout = true;

            }
        });
        back_button.setClickedListener(component -> onBackPressed());
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
